package orm

import (
	"playbook/ginsvr/src/global"
	"strings"
)

type LocalPath struct {
	UserId    string
	LocalPath string
	LocalIp   string
}

func GetLocalPath(userId string) LocalPath {
	var locPath LocalPath
	db.Table("localpath").Where("user_id = ?", userId).Find(&locPath)
	if global.OSTYPE == "windows" {
		locPath.LocalPath = strings.ReplaceAll(locPath.LocalPath, "/", global.OSTYPE)
	} else if global.OSTYPE == "linux" {
		locPath.LocalPath = strings.ReplaceAll(locPath.LocalPath, "\\", global.OSTYPE)
	}
	return locPath
}

func AddLocalPath(userId string, localPath string) Response {
	var resp Response
	err := db.Exec("INSERT INTO localpath (user_id, local_path) VALUES($1, $2)", userId, localPath)
	if err == nil {
		resp.RespCode = "000001"
		resp.RespMsg = "用户本地地址新增失败！"
	} else {
		resp.RespCode = "000000"
		resp.RespMsg = "用户本地地址新增成功！"
	}
	return resp
}

func UpdateLocalPath(userId string, localPath string) Response {
	var resp Response
	err := db.Exec("Update localpath set local_path=$1 where user_id=$2", localPath, userId)
	if err == nil {
		resp.RespCode = "000001"
		resp.RespMsg = "用户本地地址更新失败！"
	} else {
		resp.RespCode = "000000"
		resp.RespMsg = "用户本地地址更新成功！"
	}
	return resp
}
